package tech.yunland.shop.domain.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.moyuer.bsf.domain.model.User;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

/**
 * 销售单分录
 *
 * @author: 袁首京<yuanshoujing@gmail.com>
 * 16/4/13
 */
@Entity
@Table(name = "bill_sale_entry")
public class SaleBillEntry extends BillEntry {

    @JSONField(serialize = false)
    @ManyToOne
    protected SaleBill owner;

    @OneToMany(mappedBy = "owner")
    private Set<SaleBillHandler> handlers;

    @Column(nullable = false)
    private int payment = 0; // 支付方式 0 未支付 

    @Column(nullable = false)
    protected Integer state = 0; // 0 创建 1 生效(已结算)

    @JSONField(serialize = false)
    @ManyToOne
    protected User creator; // 创建人

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "create_time", nullable = false, insertable = false, updatable = false)
    protected Date createTime;

    @Transient
    protected String creatorId;
    @Transient
    protected String creatorName;

    @Transient
    private BigDecimal memberPrize;

    @Transient
    private String goodsUnit;

    public BigDecimal getMemberPrize() {
        return this.memberPrize;
    }

    public void setMemberPrize(BigDecimal memberPrize) {
        this.memberPrize = memberPrize;
    }

    @Transient
    @JSONField(format = "yyyy-MM-dd")
    private Date bizDate;

    public Date getBizDate() {
        if (this.bizDate != null) {
            return this.bizDate;
        }
        if (this.owner != null) {
            this.bizDate = owner.getBizdate();
        }
        return bizDate;
    }

    public void setBizDate(Date bizDate) {
        this.bizDate = bizDate;
    }

    public SaleBill getOwner() {
        return owner;
    }

    public void setOwner(SaleBill owner) {
        this.owner = owner;
    }

    public Set<SaleBillHandler> getHandlers() {
        return handlers;
    }

    public void setHandlers(Set<SaleBillHandler> handlers) {
        this.handlers = handlers;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCreatorId() {
        if (creatorId != null) {
            return creatorId;
        }

        if (creator != null) {
            creatorId = creator.getId();
        }

        return creatorId;
    }

    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }

    public String getCreatorName() {
        if (creatorName != null) {
            return creatorName;
        }

        if (creator != null) {
            creatorName = creator.getName();
        }

        return creatorName;
    }

    public void setCreatorName(String creatorName) {
        this.creatorName = creatorName;
    }

    public int getPayment() {
        return payment;
    }

    public void setPayment(int payment) {
        this.payment = payment;
    }


    public String getGoodsUnit() {
        return this.goods != null ? this.goods.getUnit() : this.goodsUnit;
    }

    public void setGoodsUnit(String goodsUnit) {
        this.goodsUnit = goodsUnit;
    }
}
